//
//  CalculateLoanViewController.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/18.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "CalculateLoanViewController.h"
#import "CalculateLoanMessageView.h"
#import "CalculateLoanMessageModel.h"
#import "CalculateLoanChartsView.h"
#import "CalculateLoanChartsView2.h"
#import "CalculateLoanTableView.h"
#import "Masonry.h"
#import "CalculateLoanTableViewModel.h"
#import "LabelUtil.h"
#import "CalculateLoanNumericalDataViewController.h"
#import "DetailCaculateNavView.h"
#import "Macro.h"
@interface CalculateLoanViewController ()
@property (nonatomic, strong) CalculateLoanMessageView *messageView;
@property (nonatomic, strong) CalculateLoanChartsView *chartsView;
@property (nonatomic, strong) CalculateLoanChartsView2 *chartsView2;
@property (nonatomic, strong) UILabel *averagePriceLabel;
@property (nonatomic, strong) UIButton *showNumericalDataButton;
@property (nonatomic, strong) DetailCaculateNavView  *MyNavView;
@end

@implementation CalculateLoanViewController

#pragma mark LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.8784 green:0.8941 blue:0.9412 alpha:1.0];
    self.view.frame = CGRectMake(0, 0, ScreenWidth*0.77, ScreenHeight);
    [self loadCalculateLoanView];
//    [self loadDataModel];
    __weak UIViewController *weakSelf = self;
    self.MyNavView.PayBackAction = ^{
        [weakSelf.view removeFromSuperview];
        [weakSelf removeFromParentViewController];
    };
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Event Response
- (void)showNumberDetail{
    CalculateLoanNumericalDataViewController *dataViewController = [CalculateLoanNumericalDataViewController new];
    dataViewController.loanCalculateUtil = _loanCalculateUtil;
//    [self.navigationController pushViewController:dataViewController animated:YES];
    [self.view addSubview:dataViewController.view];
    [self addChildViewController:dataViewController];
}

#pragma mark Private
- (void)loadCalculateLoanView {
    _messageView = [CalculateLoanMessageView new];
    UIView *averagePriceBackView = [UIView new];
    _chartsView = [CalculateLoanChartsView new];
    _chartsView2 = [CalculateLoanChartsView2 new];
    
    [self.view addSubview:_messageView];
    [self.view addSubview:averagePriceBackView];
    [self.view addSubview:_chartsView];
    [self.view addSubview:self.showNumericalDataButton];
    
    [self.MyNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.width.equalTo(self.view.mas_width);
        make.height.equalTo(@(59));
    }];
    
    [_messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.MyNavView).offset(TenOffsetViewMargin);
    }];
    [averagePriceBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_messageView.mas_bottom).offset(TenOffsetViewMargin);
        make.left.equalTo(self.view).offset(TenOffsetViewMargin);
        make.right.equalTo(self.view).offset(-TenOffsetViewMargin);
        make.height.mas_equalTo(ScreenHeight*0.07);
    }];
    [_chartsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(averagePriceBackView.mas_bottom);
        make.left.right.equalTo(_messageView);
        make.height.equalTo(self.view).multipliedBy(0.5);
    }];
    [self.showNumericalDataButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_chartsView.mas_bottom);
        make.left.equalTo(self.view).offset(FifteenoffsetViewMargin);
        make.right.equalTo(self.view).offset(-FifteenoffsetViewMargin);
        make.height.mas_equalTo(ScreenHeight*0.07);
    }];
    _averagePriceLabel = [LabelUtil loadLabelWithTitleLabelWithBackView:averagePriceBackView withTitle:@"月均款额" withTitleColor:TextColor60 andTitleFont:TextFont18];
    _averagePriceLabel.textColor = [UIColor redColor];
    _averagePriceLabel.textAlignment = NSTextAlignmentRight;
    _averagePriceLabel.font = TextFont18;
    
    UILabel *titleLabel = [averagePriceBackView viewWithTag:TitleLabelTag];
    [_averagePriceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(5);
        make.top.equalTo(titleLabel);
        make.right.equalTo(averagePriceBackView);
    }];
    
}


- (void)loadDataModel{
    NSMutableArray *detailData = [NSMutableArray array];
    [detailData addObject:[NSString stringWithFormat:@"%.2f万元",_loanCalculateUtil.totalRepayment]];
    [detailData addObject:[NSString stringWithFormat:@"%.2f万元",_loanCalculateUtil.loanTotalMoney]];
    [detailData addObject:[NSString stringWithFormat:@"%.2f万元",_loanCalculateUtil.totalInterest]];
    [detailData addObject:[NSString stringWithFormat:@"%ld年(%ld期)", (_loanCalculateUtil.period/12),_loanCalculateUtil.period]];
    
    NSMutableArray *messageArray = [NSMutableArray array];
    NSArray *titles = @[@"还款总额",@"贷款总额",@"支付利息",@"贷款年限"];
    for (int i =0; i<4; i++) {
        CalculateLoanMessageModel *model = [CalculateLoanMessageModel new];
        model.title = titles[i];
        model.detailTitle = detailData[i];
        [messageArray addObject:model];
    }
    _messageView.calculateLoanMessageArray = messageArray;
    _averagePriceLabel.text = [NSString stringWithFormat:@"%ld元",_loanCalculateUtil.averagePayMoneyPerMonth];
    NSMutableArray *chartArray = [NSMutableArray array];
    for (int i=0; i<_loanCalculateUtil.paymentInfos.count; i++) {
        CalculateLoanTableViewModel *model = _loanCalculateUtil.paymentInfos[i];
        [chartArray addObject:model.monthlyRepayment];
    }
    _chartsView.loanMoneyArray = chartArray;
    
}

#pragma mark - Getter
- (UIButton *)showNumericalDataButton{
    if (_showNumericalDataButton == nil) {
        _showNumericalDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_showNumericalDataButton setBackgroundColor:BackgroundColor240];
        _showNumericalDataButton.layer.masksToBounds = YES;
        _showNumericalDataButton.layer.cornerRadius = 5;
        [_showNumericalDataButton setTitle:@"显示数值数据" forState:UIControlStateNormal];
        [_showNumericalDataButton setTitleColor:IndicatorColor52 forState:UIControlStateNormal];
        _showNumericalDataButton.titleLabel.font = TextFont18;
        [_showNumericalDataButton addTarget:self action:@selector(showNumberDetail) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _showNumericalDataButton;
}

- (DetailCaculateNavView *)MyNavView {
    if (!_MyNavView) {
        _MyNavView = [[DetailCaculateNavView alloc] init];
        _MyNavView.NavTitleString = @"房贷计算器";
        _MyNavView.BtnTitleString = @"还款详情";
        [self.view addSubview:_MyNavView];
    }
    return _MyNavView;
}


@end
